﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ShoppingCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindData();
    }
    protected void btnPay_Click(object sender, EventArgs e)
    {
        try
        {
            bool insert = false;
            Basket basket = Basket.Instance;
            foreach (CartItem item in basket.Items)
            {
                SaleDAO saleDAO = new SaleDAO();
                SaleInfo sale = new SaleInfo();

                sale.PostingID = item.PostingID;
                sale.CustomerID = Session["User"].ToString();
                sale.SellDate = DateTime.Now;

                if (saleDAO.Insert(sale) > 0)
                    insert = true;


            }
            if (insert) ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Order Successfull!');", true);
            else ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Order Failed!');", true);
            basket.Items.Clear();
            Session["ASPNETShoppingCart"] = null;
        }
        catch (Exception ex) { ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Order Failed!');", true); }

    }
    protected void btnContinue_Click(object sender, EventArgs e)
    {
        Response.Redirect("Exhibitions.aspx");
    }

    protected void gvShoppingCart_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Remove")
        {
            Int64 postingID = Convert.ToInt64(e.CommandArgument);
            Basket cart = Basket.Instance;
            cart.RemoveItem(postingID);
        }
        BindData();
    }


    protected void BindData()
    {
        Basket cart = Basket.Instance;
        gvShoppingCart.DataSource = cart.Items;
        gvShoppingCart.DataBind();
    }
}